package com.jlastu.dlxy_androidapp.presenter;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.Pair;

import androidx.annotation.NonNull;

import com.jlastu.dlxy_androidapp.base.WeakRefHandler;
import com.jlastu.dlxy_androidapp.contract.StoreManageContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.entity.Store;
import com.jlastu.dlxy_androidapp.view.sell.StoreManageActivity;

import java.util.List;

public class StoreManagePresenter implements StoreManageContract.IStoreManagePresenter {
    private static final int WHAT_GET_STORE = 1;
    private StoreManageContract.IStoreManageView mStoreManageView;
    private Context mContext;
    public StoreManagePresenter(StoreManageContract.IStoreManageView storeManageView, Context context) {
        mStoreManageView = storeManageView;
        mContext = context;
    }

    @Override
    public void getStore(int user_id) {
        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                Uri uri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/store");
                uri = uri.buildUpon().appendPath(user_id+"").build();
                Log.d("uri", uri.toString());
                Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null);
//                Log.d("cursor", "cursor.getCount() ===== "  + cursor.getCount());
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        Store store = new Store();
                        store.setStore_name(cursor.getString(cursor.getColumnIndex("store_name")));
                        store.setStore_address(cursor.getString(cursor.getColumnIndex("store_address")));
//                        store.setData(cursor.getBlob(cursor.getColumnIndex("store_headimg")));
                        Message message = Message.obtain();
                        message.obj = store;
                        message.what = WHAT_GET_STORE;
                        mHandler.sendMessage(message);
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }).start();
    }

    @Override
    public void updateUser(int userid, Store store) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("store_name", store.getStore_name());
                cv.put("store_address", store.getStore_address());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/store/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的店铺 ===== " + updateCount);
                } else {
                    Log.d("", "店铺不存在");
                }
            }
        }).start();
    }

    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_STORE:
                    mStoreManageView.showStore((Store) msg.obj);
                    break;
            }
            return false;
        }
    });
}
